//
// Created by Administrator on 2021/10/29.
//
#include <jni.h>
#include <string>
#include <pthread.h>
#include <Log.h>


#define takePhotoActivity "org/yxjme/exjni_two/a/TakePhotoActivity"
#define recordVideoActivity "org/yxjme/exjni_two/a/RecordVideoActivity"



extern "C" JNIEXPORT jstring JNICALL
Java_org_yxjme_exjni_1two_ExJNI_getMessage(JNIEnv * env,jobject thiz){
    std::string hh = "000000000000000000000000000";
    LOGI("[getMessage]->",env->NewStringUTF(hh.c_str()));
    /**
     * 获取类
     */
    jclass _jclass = env->GetObjectClass(thiz);
    /**
     * 获取函数id
     */
    jmethodID _MethodID = env->GetMethodID(_jclass, "showFiled", "(Ljava/lang/String;)V");
    /**
     * 传递进去的参数
     */
    jstring  resultMsgStr  = env->NewStringUTF("this is Success");
    /**
     * 调用函数
     */
    env->CallVoidMethod(thiz,_MethodID,resultMsgStr) ;
    return env->NewStringUTF(hh.c_str());
}


extern "C"
JNIEXPORT jobject JNICALL
Java_org_yxjme_exjni_1two_ExJNI_GetOBJ(JNIEnv *env, jobject thiz, jstring jstr) {

    jclass _jclass =  env->FindClass("org/yxjme/exjni_two/ExJNI") ;
    /**
     * 获取Java类的构造方法
     */
    jmethodID mId = env->GetMethodID(_jclass, "<init>", "(Ljava/lang/String;)V");
    /**
     *
     */
    jobject obj = env->NewObject(_jclass, mId,jstr);
    return obj;
}



extern "C"
JNIEXPORT jint JNICALL
Java_org_yxjme_exjni_1two_ExJNI_arrayLength(JNIEnv *env,
        jobject thiz,
        jintArray array_list) {

    int len = env ->GetArrayLength(array_list) ;
    LOGI("len = ",len);

    jclass  cls = env->FindClass("org/yxjme/exjni_two/ExJNI");
    jmethodID mID = env->GetMethodID(cls, "fun", "(I)Ljava/lang/String;");
    env->CallObjectMethod(thiz,mID,10000) ;
    return len ;
}


extern "C"
JNIEXPORT void JNICALL
Java_org_yxjme_exjni_1two_ExJNI_createThread(JNIEnv *env, jobject thiz) {

}



void * printHelloThread(void *){
    LOGI("hello thread");
    return nullptr;
}

/**
 * 定义结构体
 */
struct threadArgs{
    int code ;
    const char *result ;
};


void toActivityMethod(JNIEnv *pEnv, jobject pJobject, jobject pJobject1, const char string[1]);

/**
 * 打印传进的数据
 * @param arg
 * @return
 */
void * threadArgsBean(void *arg){
    threadArgs * args = static_cast<threadArgs *>(arg) ;
    LOGI("this is code %d",args->code);
    LOGI("this is result %s",args->result);
    return nullptr ;
}



extern "C"
JNIEXPORT void JNICALL
Java_org_yxjme_exjni_1two_ExJNI_createJNIThread(JNIEnv *env, jobject thiz) {
    /*句柄*/
    pthread_t handle;
    threadArgs *thread = new threadArgs ;
    thread->code = 0 ;
    thread->result = "传递成功";
    /*创建线程*/
    int ret = pthread_create(&handle,nullptr,printHelloThread,threadArgsBean(thread))  ;
    if(ret == 0 ){
        LOGD("create thread success") ;
    }else{
        LOGD("create thread failed") ;
    }
}



extern "C"
JNIEXPORT void JNICALL
Java_org_yxjme_exjni_1two_ExJNI_takePhoto(JNIEnv *env, jobject thiz,jobject obj) {
    toActivityMethod(env,thiz,obj,takePhotoActivity);
}


extern "C"
JNIEXPORT void JNICALL
Java_org_yxjme_exjni_1two_ExJNI_recordVideo(JNIEnv *env, jobject t,jobject obj) {
    toActivityMethod(env,t,obj,recordVideoActivity);
}



/**
 * 公共方法
 *
 * @param pEnv
 * @param pJobject
 * @param pJobject1
 * @param str
 */
void toActivityMethod(JNIEnv *pEnv, jobject pJobject, jobject pJobject1, const char str[1]) {
    jclass  c = pEnv->FindClass("org/yxjme/exjni_two/ExJNI");
    jmethodID mId = pEnv->GetMethodID(c, "toActivity","(Landroid/content/Context;Ljava/lang/Class;)V");
    jclass  _jcls = pEnv->FindClass(str);
    pEnv->CallVoidMethod(pJobject,mId,pJobject1,_jcls) ;
}




